/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entity.Etbl_Flights;
import java.sql.CallableStatement;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author LeThuyLinh
 */
public class DALtbl_Flights extends BaseConnection{
    CallableStatement cstm=null;
    ResultSet rs=null;
    public boolean Addtbl_Flight(Etbl_Flights obj){
        try {
            getConnection();
            cstm=cnn.prepareCall("{call tbl_Flights_add(?,?,?,?,?)}");
            cstm.setString("name", obj.getName());
            cstm.setDate("departure_time",obj.getDeparture_time());
            cstm.setDate("arrival_time",obj.getArrival_time());
            cstm.setString("departure_city", obj.getDeparture_city());
            cstm.setString("arrival_city", obj.getArrival_city());
            cstm.execute();
            cstm.close();
            releaseConnection();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Flights.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public boolean updatetbl_Flight(Etbl_Flights obj){
        try {
            getConnection();
            cstm=cnn.prepareCall("{call tbl_Flights_update(?,?,?,?,?,?)}");
            cstm.setInt("flight_Id", obj.getFlight_ID());
            cstm.setString("name", obj.getName());
            cstm.setDate("departure_time",obj.getDeparture_time());
            cstm.setDate("arrival_time",obj.getArrival_time());
            cstm.setString("departure_city", obj.getDeparture_city());
            cstm.setString("arrival_city", obj.getArrival_city());
            cstm.execute();
            cstm.close();
            releaseConnection();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Flights.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public boolean deletetbl_Flight(int flight_ID){
        try {
            getConnection();
            cstm=cnn.prepareCall("{call tbl_Flights_delete(?)}");
            cstm.setInt("flight_Id", flight_ID);
            cstm.execute();
            cstm.close();
            releaseConnection();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Flights.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
     public Etbl_Flights getFilght(int flightID){
        Etbl_Flights objFlight=null;
        try {
            getConnection();
            cstm=cnn.prepareCall("{call tbl_Flights_get(?)}");
            cstm.setInt("flight_Id", flightID);
            rs=cstm.executeQuery();
            while(rs.next()){
                objFlight=new Etbl_Flights();
                objFlight.setFlight_ID(rs.getInt("flight_Id"));
                objFlight.setName(rs.getString("name"));
                objFlight.setDeparture_time(rs.getDate("departure_time"));
                objFlight.setArrival_time(rs.getDate("arrival_time"));
                objFlight.setDeparture_city(rs.getString("departure_city"));
                objFlight.setArrival_city(rs.getString("arrival_city"));
                return objFlight;
            }
            cstm.close();
            rs.close();
            releaseConnection();
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Country.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
     public ArrayList getListFlight(String orderBy, String ASCorDESC, String WhereClause, int PagingIndex, int PagingSize){
        try {
            ArrayList flightList=new ArrayList();
            getConnection();
            cstm=cnn.prepareCall("{call tbl_Flights_getpaged(?,?,?,?,?)}");
            cstm.setString("OrderBy", orderBy);
            cstm.setString("TangGiam", ASCorDESC);
            cstm.setString("WhereClause", WhereClause);
            cstm.setInt("PageIndex", PagingIndex);
            cstm.setInt("PageSize", PagingSize);
            rs=cstm.executeQuery();
            DALTotalPageIndex.getTotalPage("tbl_Flights", WhereClause, PagingSize);
            while(rs.next()){
                Etbl_Flights objFlight=new Etbl_Flights();
                objFlight.setFlight_ID(rs.getInt("flight_Id"));
                objFlight.setName(rs.getString("flight_name"));
                objFlight.setDeparture_time(rs.getDate("departure_time"));
                objFlight.setArrival_time(rs.getDate("arrival_time"));
                objFlight.setDeparture_city(rs.getString("departure_city"));
                objFlight.setArrival_city(rs.getString("arrival_city"));
                flightList.add(objFlight);
            }
            cstm.close();
            rs.close();
            releaseConnection();
            return flightList;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Cities.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
